﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class CreateCourse : Form
    {

        SynchronizeData sync = new SynchronizeData();

        GetNextID getID = new GetNextID();
        
        public CreateCourse()
        {
            InitializeComponent();
        }

        ClassDataProvider all = new ClassDataProvider();
        DataSet dsAll;
        DataTable dtClass;
        DataRow rw;
        
        private void CreateCourse_Load(object sender, EventArgs e)
        {
            ////Load thong tin lop hoc tu server cap nhat va database
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);

            ////Load thong tin khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithID(sync.CourTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.CourTb, sync.updatecol);
            
            ////Load thong tin lop hoc cua khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassInCourTb, sync.insertcol);

            ////Load thong tin mon hoc cua khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithNoneID(sync.SubInCourTb, sync.insertcol);

            dsAll = all.loadAllClass();

            dtClass = dsAll.Tables[0].Clone();

            comboBox1.DataSource = dsAll.Tables[0];
            comboBox1.DisplayMember = "NameClass";
            comboBox1.ValueMember = "ID";
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (comboBox1.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                rw = dsAll.Tables[0].Rows[int.Parse(comboBox1.SelectedIndex.ToString())];
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            dtClass.ImportRow(rw);
            gridListClass.DataSource = dtClass;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            ////Load thong tin lop hoc tu server cap nhat va database
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);

            ////Load thong tin khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithID(sync.CourTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.CourTb, sync.updatecol);

            ////Load thong tin lop hoc cua khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassInCourTb, sync.insertcol);

            ////Load thong tin mon hoc cua khoa hoc tu server cap nhat vao database
            //sync.GetDataForInsertMethodWithNoneID(sync.SubInCourTb, sync.insertcol);

            string lastID = getID.getLastID(getID.CourseTB, getID.IDCol);
            string ID = getID.NextID(lastID, getID.prefixID);
            
            int accept = 0;
            if (chkBoxStart.Checked)
                accept = 1;
            CourseDataProvider createCourse = new CourseDataProvider();
            int result = createCourse.AddCourse(ID,txtNameCourse.Text, txtStart.Text, accept);
            
            if (result == 0)
            {
                return;
            }

            ////Chuyen thong tin cua khoa hoc vua tao len server
            //string xmlCour = createCourse.ConvertInfoCourseToXml(IDCour.ToString());
            //sync.transfDataForInsertMethod(xmlCour, sync.CourTb);

            ClassInCourseProvider add = new ClassInCourseProvider();
            SubjectInCourse addSub = new SubjectInCourse();
            for (int i = 0; i < dtClass.Rows.Count; i++)
            {
                //Lay ID cua lop hoc
                string idClass = dtClass.Rows[i].ItemArray[0].ToString();
                //Add class vao ClassInCourse
                int addClass = add.addClassInCourse(ID, idClass);

                ////Chuyen thong tin lop hoc cua khoa hoc len server
                //string xmlClassInCour = add.selectClassInCourse(IDCour, idClass);
                //sync.transfDataForInsertMethod(xmlClassInCour, sync.ClassInCourTb);

                //Add vao subjectInCourse
                string idSub = dtClass.Rows[i].ItemArray[2].ToString();
                int sub = addSub.addSubInCourse(ID, idSub);

                ////Chuyen thong tin mon hoc thuoc khoa hoc len server
                //string xmlSubCour = addSub.ConvertSubInCourseToXML(IDCour, idSub);
                //sync.transfDataForInsertMethod(xmlSubCour, sync.SubInCourTb);

                if (addClass == 0 || sub == 0)
                {
                    MessageBox.Show("Tạo Khóa học không thành công");
                    return;
                }
            }
            MessageBox.Show("Tạo khóa học thành công");
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
